package com.itheima.im.socket;

import com.itheima.im.socket.bean.QQMessage;
import com.itheima.im.socket.bean.QQMessageType;
import com.itheima.im.socket.core.OnReceiveMsgListener;
import com.itheima.im.socket.core.QQConnnection;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	private QQConnnection conn = null;

	public void connect(View view) {
		new Thread() {
			public void run() {
				String ip = "192.168.15.97";
				int port = 5224;

				try {
					if (conn == null) {
						// 创建消息通道
						conn = new QQConnnection(ip, port);
						conn.addOnReceiveMsgListener(listener);
						conn.connect();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		}.start();

	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		conn.removeOnReceiveMsgListener(listener);
	}
	private OnReceiveMsgListener listener=new OnReceiveMsgListener() {
		@Override
		public void onReceive(QQMessage msg) {
			System.out.println("---监听器通过自己的响应方法来接收消息");
			System.out.println(msg.toXml());
		}
	};

	public void sendmessage(View view) {
		// 101 test 101#test
		new Thread() {
			public void run() {
				QQMessage msg = new QQMessage();
				msg.type = QQMessageType.MSG_TYPE_LOGIN;
				msg.content = "101#test";
				try {
					// String json=get();
					conn.sendMessage(msg);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			};
		}.start();
	}

	// HttURLConnection
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
